<?php
namespace BoxApi\Controller;

use Think\Controller;

class IndexController extends CommonController {
	public function UserInfo() {

	}

	/*******获得js,css资源******/
	public function getRes() {
		$allow_ext = array('js', 'css');
		$md5name = I('runThreadKey');
		$runThreadKey = md5(C('runThreadKey'));
		$url = parse_url(I('url'));
		$file = BOX_PATH . $url['path'];
		$info = explode(".", $url['path']);
		$ext = $info[count($info) - 1];
		if (!in_array($ext, $allow_ext)) {
			exit(json_encode(array('error' => 3, 'msg' => "No allow_ext")));
		}
		if (is_file($file)) {
			$content = file_get_contents($file);
		} else {
			exit(json_encode(array('error' => 2, 'msg' => "No file")));
		}
		if ($md5name == $runThreadKey) {
			header("Access-Control-Allow-Origin: *");
		}
		exit(json_encode(array('error' => 0, 'msg' => $content)));
	}

	/********网站初始化参数***/
	public function get_init_param() {

		if ($this->is_mobile) {
			Logic("BoxSns", "Member")->set_login();
			$this->user_id = session('user_id');
		}

		$appName = I('appName');
		$view_type = I('view_type', "web");
		$param = array(
			'appName' => I('appName'),
		);
		$data['params'] = array(
			'debug' => false, //是否调试
			'BoxVersion' => '2.8', //版本号
			'BoxVersionCache' => '2.4', //缓存号，重新发布
			'PageVersionCache' => 2, //页面缓存
			'view_type' => $view_type,
		);
		$data['params']['test'] = session("user_id");
		if ($this->user_id) {
			$data['user_info'] = BoxModel("system_member")->load($this->user_id);
			$data['uuid'] = $data['user_info']['uuid'];
		}
		$params = (array) dict($appName . 'Params');
		$data['params'] = array_merge($data['params'], $params);
		if ($data['params']['debug'] == "true") {
			$data['params']['BoxVersion'] = $data['params']['BoxVersion'] . time();
			$data['params']['PageVersionCache'] = $data['params']['PageVersionCache'] . time();
			$data['params']['BoxVersionCache'] = $data['params']['BoxVersionCache'] . time();
		}
		$params2 = dict($appName . 'Params' . $view_type);
		//移动
		if ($params2) {
			$data['params'] = array_merge($data['params'], (array) $params2);
		}

		hook("get_init_param");
		if ($param['appName']) {
			hook("get_init_param_" . $param['appName'], $data);
		}
		exit(json_encode($data));
	}

	public function logout() {
		Logic("BoxSns", "member")->logout();
		$this->jump("", "/");
	}

	public function auth_info() {
		$box_login_code = I('box_login_code');
		$data = Logic("BoxSns", "Member")->auth_info($box_login_code);
		echo json_encode($data);
	}

	public function noti() {
		// dump($this->user_id);
		if (!$this->user_id) {
			echo json_encode(array('error' => 1, 'msg' => '请先登录'));
		} else {
			$msg_count = BoxModel("sns_notification_count")->get(array('user_id' => $this->user_id));
			$type = array();
			foreach ($msg_count as $one) {
				$type[$one['type']] = $one['count'];
			}
			$list = BoxModel("sns_notification")->get(array('user_id' => $this->user_id, 'state' => 0), '5');
			echo json_encode(array('msg_count' => $type, 'list' => $list['list']));
		}
	}

	/********二维码**********/
	function QRcode() {
		$url = I('url');
		$QRcode = new \Common\Org\QRcode();
		ob_start();
		ob_clean();
		$QRcode->img(urldecode($url));
	}

	//用户信息
	function GetUserInfoByToken() {
		$this->CheckAppSecret();
		$user_info = array(
			'nickname' => 'wen190',
			'headimg' => 'http://wx.qlogo.cn/mmopen/h8pg0yFfWYVu0QLrKhAf2xPmXDFyzibxtdjeK0s3AVFnY357ia30eBzFLHBe6FniaquM6W9DgGtwNyd2FxnLhkiaTS539oaBwRxD/0',
			'xuebazi' => 178,
		);
		echo json_encode(array('error' => 0, 'msg' => '成功', 'data' => $user_info));
	}
	//是否开通会计
	function CheckAccountantByToken() {
		$this->CheckAppSecret();
		$data = array('open' => 1);
		echo json_encode(array('error' => 0, 'msg' => '成功', 'data' => $data));
	}
	//完成会计题
	function FinishAccountantExamination() {
		$this->CheckAppSecret();
		echo json_encode(array('error' => 0, 'msg' => '成功'));
	}

}
